In [1]:
# Author: Stephen Situ
# A common data task that needs to be done frequently is to automate the acquisition of data with a script. To do this, a tool like
# linux cronjobs can be used to schedule scripts to run on schedule. For windows, a tool like windows task scheduler can be used. 
# In this project, a python script reads data from the yfinance API, puts it a panda's dataframe, and the time is written to a log file.
# Using cron, we can schedule cronjobs using the Linux Bash Language. The crontab format for schedule is minute (0-59). hour (0-23)
# day of month (1-31), month (1-12), day of week (0-7). An * indicates that it takes any value. For example, * * * * * tells cron
# to run the script every minute of every hour of every day of month of every month of every day of week. Afterwards, the bash line
# "python3 daily_stock_reader.py" executes the script. 

# It is also important to check permissions on the file to execute, this can be done with "chmod +x daily_stock_reader.py"
# To check permissions afterwards, you can use "ls -l daily_stock_reader.py"
# rwx indicates the file has read, write, and execute permissions for the owner, group owner, and all other users. 
In [ ]:
# load libraries
import pandas as pd
import numpy as np
import yfinance as yf
import datetime 
In [2]:
# set stock data to object
TESLA = yf.Ticker('TSLA')
In [3]:
# Use history method and assign to df
df = TESLA.history(start = '2020-01-01')
In [4]:
# print dataframe
print(df)
                                 Open        High         Low       Close  \
Date                                                                        
2020-01-02 00:00:00-05:00   28.299999   28.713333   28.114000   28.684000   
2020-01-03 00:00:00-05:00   29.366667   30.266666   29.128000   29.534000   
2020-01-06 00:00:00-05:00   29.364668   30.104000   29.333332   30.102667   
2020-01-07 00:00:00-05:00   30.760000   31.441999   30.224001   31.270666   
2020-01-08 00:00:00-05:00   31.580000   33.232666   31.215334   32.809334   
...                               ...         ...         ...         ...   
2023-01-09 00:00:00-05:00  118.959999  123.519997  117.110001  119.769997   
2023-01-10 00:00:00-05:00  121.070000  122.760002  114.919998  118.849998   
2023-01-11 00:00:00-05:00  122.089996  125.949997  120.510002  123.220001   
2023-01-12 00:00:00-05:00  122.559998  124.129997  117.000000  123.559998   
2023-01-13 00:00:00-05:00  116.550003  122.629997  115.599998  122.400002   

                              Volume  Dividends  Stock Splits  
Date                                                           
2020-01-02 00:00:00-05:00  142981500          0           0.0  
2020-01-03 00:00:00-05:00  266677500          0           0.0  
2020-01-06 00:00:00-05:00  151995000          0           0.0  
2020-01-07 00:00:00-05:00  268231500          0           0.0  
2020-01-08 00:00:00-05:00  467164500          0           0.0  
...                              ...        ...           ...  
2023-01-09 00:00:00-05:00  190284000          0           0.0  
2023-01-10 00:00:00-05:00  167642500          0           0.0  
2023-01-11 00:00:00-05:00  183810800          0           0.0  
2023-01-12 00:00:00-05:00  169400900          0           0.0  
2023-01-13 00:00:00-05:00  180439300          0           0.0  

[765 rows x 7 columns]
In [5]:
# Get date and time
now = datetime.datetime.now()
In [6]:
# Set file name with format
filename = now.strftime("%Y-%m-%d %H-%M-%S") + '.csv'
In [7]:
# Write to csv file
df.to_csv(filename, index=False)
In [13]:
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")

with open("Stock_time.txt", "a") as log_file:
    log_file.write(f"{timestamp} - Stock Price Record time. \n")